// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Layouts
import Fk
import Fk.Components.LunarLTK
import Fk.Pages.LunarLTK
import Fk.Components.Common

GraphicsBox {
  id: root
  property var curchoices: []
  property var op_curchoices: []
  property var all_dialog: []
  property string eventname: ""
  property string curdialog: ""
  property var eventchc: []
  property bool branchchoice: false
  property int cardid: 0
  property int startindex: 0
  property int chsindexs: 0
  property int lastdialoginx: 0

  title.text: eventname
  width: 550
  height: leftman.height + title.height + decontent.height + 50

  Flickable {
    id: decontent
    x:10
    anchors.top:title.bottom
    anchors.topMargin: 10
    contentHeight: 130
    width: parent.width - 10
    height: curdialog.height
    clip: true
    Text {
      id: curdialog
      width: parent.width
      // lineHeight: 10
      // lineHeightMode: Text.FixedHeight
      // height: parent.height
      text: ""
      color: "white"
      wrapMode: Text.WordWrap
      font.pixelSize: 16
      textFormat: TextEdit.RichText
    }
  }

  GeneralCardItem {
    anchors.top:decontent.bottom
    anchors.topMargin: 10
    anchors.left:decontent.left
    anchors.leftMargin: 10
    id: leftman
    name: "caocao"
    visible: name !== ""
  }

  GeneralCardItem {
    anchors.top:decontent.bottom
    anchors.topMargin: 10
    anchors.right:decontent.right
    anchors.rightMargin: 10
    id: rightman
    name: "caocao"
    visible: name !== ""
  }

  CardItem {
    id:card
    visible:cardid !== 0
    anchors.top:decontent.bottom
    anchors.topMargin: 10
    anchors.right:rightman.left
    anchors.rightMargin: 80
    scale: 0.85
    Component.onCompleted: {
      if(cardid !== 0){
        setData(Lua.call("GetCardData", cardid));
      }
    }
  }

  Flickable {
    id: choiceContainer
    // ScrollBar.horizontal: ScrollBar {}

    flickableDirection: Flickable.AutoFlickIfNeeded
    width: 300
    height: 350
    // anchors.fill: parent
    anchors.top:decontent.bottom
    anchors.topMargin: 30
    anchors.left:leftman.right
    anchors.leftMargin: 20

    contentWidth: body.width
    contentHeight: body.height
    clip: true

    GridLayout {
      id: body
      x:2

      Layout.alignment: Qt.AlignHCenter
      flow: GridLayout.TopToBottom
      columns: 3
      rows: 3
      columnSpacing: 5
      rowSpacing: 5

      Repeater {
        model: curchoices

        MetroButton {
          Layout.fillWidth: true
          text: modelData
          // visible: canchoices || curchoices.indexOf(modelData) === 0
          enabled: op_curchoices.length === 0 || op_curchoices.indexOf(modelData) !== -1
          textFont.pixelSize: 18

          onClicked: {  
            if (!branchchoice){
              chsindexs = index
            }                   
            if(modelData === "继续" || modelData === "下一章") {
              updateNextDialog()
            }else{
              if(eventchc.length > 0){
                return replyResult(modelData,eventchc[index])
              }else{
                updateNextDialog()
              }
            }
          }
        }
      }
    }
  }

  function replyResult(chsret,chcevent) {
    let choices = all_dialog[startindex].choices
    const reply ={
        startpos: startindex,
        selchcinx: chsindexs,
        selchc: chsret ?? "",
        chcevent: chcevent ?? 0, //本次剧情事件结束，准备跳到lua进入下一章剧情
        cid: cardid ?? 0,
      };
    ClientInstance.replyToServer("",reply); 
    root.close();
  }

  function updateNextDialog() {
    let curdialogdata = all_dialog[startindex+1] 
    if (!curdialogdata) {
      // console.log("========qml当前对话章节结束==========")
      replyResult("下一章")
      return 
    }
    startindex = startindex + 1

    updateCurData()
  }

  function updateCurData(){
    let curdialogdata = all_dialog[startindex]
    let choices = curdialogdata.choices
    let dialog = curdialogdata.dialog
    // leftman.name = dialog[chsindexs].talkers[0] === 1 ? dialog[chsindexs].talkers[1] : dialog[chsindexs].talkers[3];
    // rightman.name = dialog[chsindexs].talkers[2] === 2 ? dialog[chsindexs].talkers[3] : dialog[chsindexs].talkers[1];
    if (dialog[chsindexs].talkers[0] === 1) {
      leftman.name = dialog[chsindexs].talkers[1]
      rightman.name = dialog[chsindexs].talkers[3]
      rightman.opacity = 0.3
      leftman.opacity = 1
    }else{
      leftman.name = dialog[chsindexs].talkers[3]
      rightman.name = dialog[chsindexs].talkers[1]
      leftman.opacity = 0.3
      rightman.opacity = 1
    }
    branchchoice = curdialogdata.branchchoice ?? false
    eventchc = curdialogdata.eventchc ?? []
    curdialog.text = dialog[chsindexs].say
    curchoices = branchchoice ? [choices[chsindexs]] : choices
    op_curchoices = curdialogdata.op_choices ?? []
    cardid = curdialogdata.cid ?? 0
    if(cardid !== 0){
      card.setData(Lua.call("GetCardData", cardid));
    }
  }

  function loadData(data) {
    // console.log(data)
    eventname = data[0];

    all_dialog = data[1];
    startindex = data[2];
    chsindexs = data[3];

    updateCurData()
  }
}
